package indi.kider.server.woniubus.api;

import indi.kider.common.constant.EmployeeRoleCons;
import indi.kider.common.model.apartment.Community;
import indi.kider.common.model.apartment.HousesMsg;
import indi.kider.common.model.employee.Employee;
import indi.kider.common.model.woniubus.HouseVo;
import indi.kider.server.apartment.model.HousesCommunityQuery;
import indi.kider.server.woniubus.pageModel.CommunityPageQuery;
import indi.kider.server.woniubus.service.HouseService;
import indi.kider.server.woniubus.service.related.AuthcService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import pers.vankid.common.model.base.CommonResponse;
import pers.vankid.common.utils.page.PageRequest;

import java.util.List;
import java.util.Map;

/**
 * 公寓 房源控制器
 * 二期新改接口
 */
@RestController
@RequestMapping(value = "house")
public class HouseController {

    @Autowired
    private HouseService houseService;

    @Autowired
    AuthcService authcService;

    /**
     * @api {POST} houseList
     * @apiGroup house
     * @apiDescription 小区（分页）
     * @apiParamExample 请求样例：
     * http://localhost:9002/woniubus/house/houseList
     * {"num":"0","size":"8","query":{"state":-1,"expression":"1","communityId":-1}}
     * @apiSuccessExample {json} 返回样例:
     * {"status":"200","msg":"查询成功！","data":{"rows":[{"communityId":1,"title":"天安门城楼·紫荆城","count":1,"roomList":[{"manSize":1,"womanSize":2,,"houseId":1,"houseNumb":"1","houseDecs":"","priceSet":"","state":1,"stateName":"已租","rent":{"id":1,"housesId":1,"communityId":1,"tenantId":1,"rentDate":"2018-05-15 00:00:00","state":1,"remark":"1","priceSet":"123","setLease":123,"leaseType":"12","deposit":"123","depositRemark":"123","initWaterRead":"132","initPowerRead":"123","payCycle":1,"wxopenId":null,"tenantName":"2123","tenantTel":"123","houseNumb":"123","building":"123","unit":"132","address":"123","community":"123","release":1}}],"open":false}],"total":1,"size":0}}
     */
    @PostMapping("/houseList")
    public CommonResponse houseList(@RequestBody PageRequest<HousesCommunityQuery> request) {
        Employee employee = authcService.getCurrentUser().getData();
        if (employee != null && !employee.getRoles().equals(EmployeeRoleCons.ADMIN)) {//超级管理员
            request.getQuery().setEnterpriseId(employee.getEnterpriseId());
        }
        return CommonResponse.buildDefaultSuccessResponse(houseService.houseList(request));
    }

    /**
     * 公寓列表
     * http://localhost:9002/woniubus/house/communityList
     * @param request
     * @return
     */
    @PostMapping("/communityList")
    public CommonResponse communityList(@RequestBody PageRequest<CommunityPageQuery> request) {
        Employee employee = authcService.getCurrentUser().getData();
        if (employee != null && !employee.getRoles().equals(EmployeeRoleCons.ADMIN)) {//超级管理员
            request.getQuery().setEnterpriseId(employee.getEnterpriseId());
        }
        return CommonResponse.buildDefaultSuccessResponse(houseService.communityList(request));
    }
    /**
     * 设置房间入住人数
     * http://localhost:9002/woniubus/house/getHouseUserCntMap
     * @param houseId
     * @return
     */
    @PostMapping("/getHouseUserCntMap")
    public CommonResponse getHouseUserCntMap(@RequestBody String houseId){
        try {
            houseService.getHouseUserCntMap(Long.parseLong(houseId));
            return CommonResponse.buildDefaultSuccessResponse("");
        }catch (Exception e){
            return CommonResponse.buildExceptionResponse(e);
        }
    }

    /**
     * @api {POST}/queryCommunityList
     * @apiGroup house
     * @apiDescription 获取公寓列表
     * @apiParamExample 请求样例：
     * http://localhost:9002/woniubus/house/queryCommunityList
     * @apiSuccessExample {json} 返回样例:
     * {
     * "status": "200",
     * "msg": "查询成功！",
     * "data": [
     * {
     * "id": 1,
     * "city": "杭州市",
     * "area": "西湖区",
     * "community": "万科西庐",
     * "address": "蒋村",
     * "coordinate": "xxx",
     * "remark": "xxx",
     * "aroundDirs": "xxxxx"
     * }
     * ]
     * }
     */
    @PostMapping("/queryCommunityList")
    public CommonResponse<List<Community>> queryCommunityList() {
        Employee employee = authcService.getCurrentUser().getData();
        return CommonResponse.buildGetSuccessResponse(houseService.queryCommunityList(employee));
    }

    /**
     * 获取公寓带租客人数列表
     * @return
     */
    @PostMapping("/queryCommunityTenantList")
    public CommonResponse<List<Community>> queryCommunityTenantList() {
        Employee employee = authcService.getCurrentUser().getData();
        return CommonResponse.buildGetSuccessResponse(houseService.queryCommunityTenantList(employee));
    }

    /**
     * 根据公寓id获取房间列表
     * @param communityId
     * @return
     */
    @PostMapping("/queryHouseList")
    public CommonResponse<List<Map>> queryHouseList(@RequestBody String communityId) {
        try {
            return CommonResponse.buildGetSuccessResponse(houseService.queryHouseList(Long.parseLong(communityId)));
        }catch (Exception e){
            return CommonResponse.buildExceptionResponse(e);
        }
    }
    /**
     * @api {GET} /getHouseById/{houseId}
     * @apiGroup house
     * @apiDescription 根据房屋id获取房屋的详细信息
     * @apiParamExample 请求样例：
     * http://localhost:9002/woniubus/house/getHouseById/1
     * @apiSuccessExample {json} 返回样例:
     * {
     * "status": "200",
     * "msg": "查询成功！",
     * "data": {
     * }
     * }
     */
    @GetMapping("/getHouseById/{houseId}")
    public CommonResponse<HouseVo> getHouseById(@PathVariable Long houseId) {
        return CommonResponse.buildGetSuccessResponse(houseService.getHouseById(houseId));
    }
}
